package cn.hsw.manage.user.controller;


import cn.hsw.component.core.dto.PageDto;
import cn.hsw.component.core.dto.ResultDto;
import cn.hsw.component.core.util.ResultUtil;
import cn.hsw.component.data.page.PageUtil;
import cn.hsw.component.web.base.BaseController;
import cn.hsw.manage.user.qo.UserSearchQo;
import cn.hsw.manage.user.service.UserService;
import cn.hsw.manage.user.vo.UserDetailVo;
import cn.hsw.manage.user.vo.UserListVo;
import cn.hsw.manage.user.vo.UserVO;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 李子木
 * @since 2021-11-03 08:55:22
 */
@RestController
@RequestMapping("/manage/user")
public class UserController extends BaseController {

    @Autowired
    UserService service;

    @PostMapping("/add")
    @RequiresPermissions("manage:user:add")
    public ResultDto add(@Validated UserVO userVO){
        service.add(userVO);
        return ResultUtil.success();
    }

    @PostMapping("/del/{pid}")
    @RequiresPermissions("manage:user:del")
    public ResultDto del(@PathVariable("pid")Long pid){

        service.deleteByPid(pid);

        return ResultUtil.success();
    }

    @PostMapping("/edit/{pid}")
    @RequiresPermissions("manage:user:edit")
    public ResultDto<Object> edit(@PathVariable("pid")Long pid,
                                  @RequestParam(value = "realName",required = false)String realName, @RequestParam(required = false) String remark,
                                  @RequestParam(value = "rolePid",required = false)String rolePid){

        service.edit(pid,realName,remark,rolePid);

        return ResultUtil.success();
    }

    @PostMapping("/list")
    @RequiresPermissions("manage:user:list")
    public ResultDto<PageDto<UserListVo>> list(UserSearchQo params){
        IPage<UserListVo> page = service.findByUserNameAndPhoneLike(params.getUserName(),params.getPhone(),PageUtil.getPage(params.getPage(),params.getSize()));
        return ResultUtil.success(page.getRecords(),page.getTotal());
    }

    @PostMapping("/reset/password/{pid}")
    @RequiresPermissions("manage:user:reset:password")
    public ResultDto<String> edit(@PathVariable("pid")Long pid){

        String password = service.resetPassword(pid);

        return ResultUtil.success(password);
    }

    @PostMapping("/info/{pid}")
    @RequiresPermissions("manage:user:info")
    public ResultDto<UserDetailVo> info(@PathVariable("pid")Long pid){

        UserDetailVo result = service.findByPid(pid);

        return ResultUtil.success(result);
    }
}
